<?php

/**
 * ECSHOP 专题
 * ============================================================================
 * 版权所有 2005-2010 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: liubo $
 * $Id: article.php 16455 2009-07-13 09:57:19Z liubo $
*/

define('IN_ECS', true);
define('ECS_ADMIN', true);

require(dirname(__FILE__) . '/includes/init.php');

$act = !empty($_GET['act']) ? $_GET['act'] : '';
//if ($_SESSION['user_id'] > 0)
//{
//	$smarty->assign('user_name', $_SESSION['user_name']);
//
//}
//if (!function_exists("htmlspecialchars_decode"))
//{
//	function htmlspecialchars_decode($string, $quote_style = ENT_COMPAT)
//	{
//		return strtr($string, array_flip(get_html_translation_table(HTML_SPECIALCHARS, $quote_style)));
//	}
//}

/*专题热门列表*/

if ($act == 'get_hot'){
// 	$pageNo = !empty($_REQUEST['pageNo']) ? intval($_REQUEST['pageNo']) : 1;
//	
//	$pageSize=!empty($_REQUEST['pageSize'])?intval($_REQUEST['pageSize']) :20;
//
//	$cat =!empty($_REQUEST['cat'])?intval($_REQUEST['cat']) :0;
			
	$sql = " select t.topic_id,t.title_pic,t.state,tc.cat_name from " . $ecs->table('topic') . ' AS t LEFT JOIN '
				 . $ecs->table('topic_cat') . ' AS tc ON t.cat_id= tc.cat_id '; 
	$sql .= " WHERE t.is_open = 1 AND (t.is_hot > 0 OR t.is_top > 0) ";
	$sql .= " ORDER BY t.topic_id DESC";
//	$sql .= " limit " .($pageNo-1)*$pageSize." ,".$pageSize;				
			
	$row =$GLOBALS['db']->getAll($sql);
//	var_dump($row);
//	$topic_array = array();
//	$topic_cat = 0;
	foreach ($row as $k => $v)
	{
//		if ($topic_cat != $v['cat_id'])
//		{
//			$topic_array[$v['cat_id']]['cat_id'] = $v['cat_id'];
//			$topic_array[$v['cat_id']]['cat_name'] = $v['cat_name'];	
//			$topic_array[$v['cat_id']]['data'] = array();				
//		}
//		$topic['topic_id'] = $v['topic_id'];
		$v['title_pic'] = OSS_HOST.$v['title_pic'];
		if ($v['state'] == 1)
		{
			$v['state'] = "未开始";
		}
		elseif ($v['state'] == 2) 
		{
			$v['state'] = "进行中";
		}
		elseif ($v['state'] == 3) 
		{
			$v['state'] = "已结束";						
		}
		else
		{
			$v['state'] = "";									
		}		
//		array_push($topic_array[$v['cat_id']]['data'],$topic);
//		$topic_cat = $v['cat_id'];

		$row[$k] = $v;
	}

	echo json_encode($row);
}

/*专题列表*/

else if ($act == 'get_list'){

   	$pageNo = !empty($_REQUEST['pageNo']) ? intval($_REQUEST['pageNo']) : 1;	
	$pageSize=!empty($_REQUEST['pageSize'])?intval($_REQUEST['pageSize']) :20;

//	$cat =!empty($_REQUEST['cat'])?intval($_REQUEST['cat']) :0;
			
	$sql = " select t.topic_id,t.title_pic,t.state,tc.cat_name from " . $ecs->table('topic') . ' AS t LEFT JOIN '
				 . $ecs->table('topic_cat') . ' AS tc ON t.cat_id= tc.cat_id '; 
	$sql .= " WHERE t.is_open = 1 ";
	$sql .= " ORDER BY t.topic_id DESC";
	$sql .= " limit " .($pageNo-1)*$pageSize." ,".$pageSize;				
			
	$row =$GLOBALS['db']->getAll($sql);
	foreach ($row as $k => $v)
	{

		$v['title_pic'] = OSS_HOST.$v['title_pic'];
		if ($v['state'] == 1)
		{
			$v['state'] = "未开始";
		}
		elseif ($v['state'] == 2) 
		{
			$v['state'] = "进行中";
		}
		elseif ($v['state'] == 3) 
		{
			$v['state'] = "已结束";						
		}
		else
		{
			$v['state'] = "";									
		}

		$row[$k] = $v;
	}

	echo json_encode($row);
}

/*专题内容请求*/
 else if ($act == 'get_info'){

 	$id = !empty($_REQUEST['id']) ? intval($_REQUEST['id']) : '';

	if ($id > 0)
	{
		$sql = " select t.topic_id,t.topic_img,t.title,t.intro as content ,t.description,tc.cat_type from " . $ecs->table('topic') . ' AS t LEFT JOIN '
					 . $ecs->table('topic_cat') . ' AS tc ON t.cat_id= tc.cat_id WHERE topic_id = ' . $id . ' AND is_open = 1';		
		$topic_row =$GLOBALS['db']->getRow($sql);					 
		if (!empty($topic_row))
		{
			if ($topic_row['cat_type']==0)
			{
				$result['related'] = get_related_article($topic_row['topic_id']);							
			}
			elseif ($topic_row['cat_type']==1)
			{
				$result['related'] = get_related_goods($topic_row['topic_id']);							
			}			
			$topic_row['topic_img'] = OSS_HOST.$topic_row['topic_img'];			
			$topic_row['content'] = str_replace('<img src="/', '<img src="'.OSS_HOST , $topic_row['content']);		
			//检测文章内容是否插入产品
			$topic_row['content'] = insert_goods($topic_row['content']);						
//			unset($topic_row['cat_type']);
			$result['info'] = $topic_row;						
		}
	}
	echo json_encode($result);

	
 }

/**
 * 获得关联的文章
 *
 * @access  public
 * @param   integer $id
 * @return  array
 */
function get_related_article($id, $limit='')
{	
    /* 相关文章ID */
    $sql = "SELECT article_id FROM " . $GLOBALS['ecs']->table('topic_article') . " WHERE topic_id = '$id'";
    $row = $GLOBALS['db']->getAll($sql);	
	$article_id_arr = array_column($row, 'article_id');
	    	
	/* 获取文章内容 */
	$sql = " select a.title,a.description,a.click_count,a.article_id,a.file_url as img_url,ac.cat_name,ac.show_type,cr.copyright_type from " . $GLOBALS['ecs']->table('article') . ' AS a LEFT JOIN '
				 . $GLOBALS['ecs']->table('article_cat') . ' AS ac ON a.cat_id= ac.cat_id '
				 .' LEFT JOIN '. $GLOBALS['ecs']->table('copyright') . ' AS cr ON a.copyright_id = cr.copyright_id' ; 
	$sql .= " WHERE a.is_open = 1 AND a.article_id <> '$id' AND a.article_id ".db_create_in(array_unique($article_id_arr));
	$sql .= " ORDER BY a.add_time desc ";
    $row = $GLOBALS['db']->getAll($sql);
	if ($limit){
		$sql .= " limit " .$limit;							
	}
    $row = $GLOBALS['db']->getAll($sql);		
	
//	var_dump($row);		
	foreach ($row as $key => $article)
	{
		//文章头图
		$article['img_url'] = OSS_HOST.get_image_path($article['article_id'],$article['img_url'],false,'artile');
		$article['copyright_type'] == 1 ? $article['is_original'] = 1 : $article['is_original'] = 0;
		$row[$key] = $article;		
	}
			
    return $row;
}

/**
 * 文章内插入商品情况
 *
 * @access  public
 * @param   integer $content
 * @param   integer $type	1评测；2资讯
 * @return  array
 */
function insert_goods($content)
{
	$result = array();
	$part_array = explode('"></a>',$content);
//	var_dump($part_array);
	for($i=0; $i<count($part_array); $i++)
	{
		$mark_str = '<a name="';		
		$mark_pos = strpos($part_array[$i],$mark_str);
		if ($mark_pos)
		{//查找到商品替换标签
			$part_content = substr($part_array[$i],0,$mark_pos);
			$goods_id = substr($part_array[$i],$mark_pos+strlen($mark_str));
			if ($goods_id)
			{
//				$goods_id = substr($part_goods,strlen());
				$part_goods = get_goods($goods_id);
			}
			array_push($result,array("content"=>$part_content,"goods"=>$part_goods));			
		}
		else
		{
			array_push($result,array("content"=>$part_array[$i],"goods"=>""));
		}
	}
//	var_dump($result);	
	return $result;
//	if (strstr($result['content'],"<a name="))
//	{
//		$news_part;
//		$part_content = strstr($result['content'],"<a name='");
//		$part_end = strpos(strstr($result['content'],"<a name=",false),"'></a>");		
//	}
}

/**
 * 获得商品数据
 *
 * @access  public
 * @param   integer $id
 * @return  array
 */
function get_goods($id)
{	
    /* 相关商品 */
    $sql = "SELECT g.goods_id, g.goods_name, g.goods_img, g.shop_price as price, g.level, g.intelligent, g.property " .
            "FROM " . $GLOBALS['ecs']->table('goods') . " AS g " .
            "WHERE g.goods_id = '$id' AND g.is_delete = 0 ";
    $good = $GLOBALS['db']->getRow($sql);
	
	if($good)
	{
		$good['level']>0 ? $good['level'] = $GLOBALS['db']->getOne('SELECT lable_title FROM '.$GLOBALS['ecs']->table('label')." WHERE label_id = ".$good['level']) : $good['level'] = "";
		$good['intelligent']>0 ? $good['intelligent'] = $GLOBALS['db']->getOne('SELECT lable_title FROM '.$GLOBALS['ecs']->table('label')." WHERE label_id = ".$good['intelligent']) : $good['intelligent'] = "";
		$good['property']>0 ? $good['property'] = $GLOBALS['db']->getOne('SELECT lable_title FROM '.$GLOBALS['ecs']->table('label')." WHERE label_id = ".$good['property']) : $good['property'] = "";
		//校正图片地址
		$good['goods_img'] = OSS_HOST.get_image_path($good['goods_id'],$good['goods_img'],false,'goods');		

		$good['price'] = (int)$good['price'];
		
	}
			
    return $good;
}

/**
 * 获得专题关联的商品
 *
 * @access  public
 * @param   integer $id
 * @return  array
 */
function get_related_goods($id, $limit='')
{	
    /* 相关商品 */
    $sql = "SELECT a.goods_id, g.goods_name, g.goods_img, g.shop_price as price, g.level, g.intelligent, g.property " .
            "FROM " . $GLOBALS['ecs']->table('topic_goods') . " AS a, " . $GLOBALS['ecs']->table('goods') . " AS g " .
            "WHERE a.goods_id = g.goods_id AND g.is_delete = 0 " .
            "AND a.topic_id = '$id'"; //LIMIT 3
    $row = $GLOBALS['db']->getAll($sql);
	
	foreach ($row as $key => $good)
	{
		$row[$key]['level'] = '';		
		if ($good['level']>0)
		{
			$row[$key]['level'] = $GLOBALS['db']->getOne('SELECT lable_title FROM '.$GLOBALS['ecs']->table('label')." WHERE label_id = ".$good['level']);
		}
		$row[$key]['intelligent'] = '';		
		if ($good['intelligent']>0)
		{
			$row[$key]['intelligent'] = $GLOBALS['db']->getOne('SELECT lable_title FROM '.$GLOBALS['ecs']->table('label')." WHERE label_id = ".$good['intelligent']);
		}
		$row[$key]['property'] = '';
		if ($good['property']>0)
		{
			$row[$key]['property'] = $GLOBALS['db']->getOne('SELECT lable_title FROM '.$GLOBALS['ecs']->table('label')." WHERE label_id = ".$good['property']);
		}				
		//校正图片地址
		$row[$key]['goods_img'] = OSS_HOST.get_image_path($id,$good['goods_img'],false,'goods');		

		$row[$key]['good_count'] = 10;	
		$row[$key]['price'] = (int)$good['price'];
		
	}
			
    return $row;
}

?>